package com.hyh.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.hyh.admin.domain.resources.Resources;
import com.hyh.admin.domain.resources.ResourcesViews;
import com.hyh.admin.mapper.resources.ResourcesViewsMapper;
import com.hyh.admin.service.IResourcesViewsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;


/*
 *  资源历史记录-实现类
 */
@Service
public class ResourcesViewsServiceImpl implements IResourcesViewsService {

    @Autowired
    private ResourcesViewsMapper resourcesViewsMapper;

    @Override
    public List<Resources> selectResourcesViewsByUserId(Long userId) {
        return resourcesViewsMapper.selectResourcesViewsByUserId(userId);
    }

    @Override
    public int deleteResourcesViews(Long resourceId) {

        return resourcesViewsMapper.deleteById(resourceId);
    }

    @Override
    public int insertResourcesViews(ResourcesViews resourcesViews) {
        //查询数据是否存在
        LambdaQueryWrapper<ResourcesViews> queryWrapper = new LambdaQueryWrapper<>();
        ResourcesViews resourcesViews1 = resourcesViewsMapper.selectOne(queryWrapper.eq(ResourcesViews::getUserId, resourcesViews.getUserId()).eq(ResourcesViews::getResourcesId, resourcesViews.getResourcesId()));
        if (resourcesViews1 != null) {
            //更新时间
            resourcesViews1.setUpdateTime(new Date());
            return resourcesViewsMapper.updateById(resourcesViews1);
        }
        return resourcesViewsMapper.insert(resourcesViews);
    }
}
